#pragma once
#include <nodes\SpriteNode.h>
#include <utils\mtrand.h>
#include <base\Controller.h>
#include "Map.h"

class Block : public ds::SpriteNode {

struct Piece {
	ds::SHandle handle;
	uint32 color;
};

public:
	Block(Map* map);
	~Block(void);
	void loadContent( ds::ResManager& resourceManager );
	void update( float elapsed );
	int getColor(int idx) const {
		return m_Pieces[idx].color;
	}
	void pickColors();
	void rotateColors();
	bool convert();
	void setActive(const bool active) {
		for ( int i = 0; i < 4; ++i ) {
			setVisible(m_Pieces[i].handle,active);
		}
		setVisible(m_Border,active);
		ds::SpriteNode::setActive(active);
	}
private:
	void updatePosition(const ds::Vec2& pos);
	int getRnd() {
		return (int)(rand()*4.0f);
	}
	int getRnd(int max) {
		return (int)(rand()*(float)max);
	}
	void setColor(int idx,int color) {
		m_Pieces[idx].color = color;
		int x = color * 34;
		setOffset(m_Pieces[idx].handle,72,x,32,32);
	}
	//
	ds::Vec2 m_Position;
	ds::SHandle m_Border;
	MTRand_open rand;
	Piece m_Pieces[4];
	Map* m_Map;
};

